package com.jxwifi.cloud.quickclean.utils;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.idroid.utils.PxUtils;
import com.idroid.wheelview.OnWheelChangedListener;
import com.idroid.wheelview.WheelView;
import com.idroid.wheelview.adapters.AbstractWheelAdapter;

import java.util.List;

import cn.trinea.android.common.util.ListUtils;

/**
 *
 * Created by zhangbo on 2017/4/1.
 */

public abstract class BaseWheelController<T> implements OnWheelChangedListener {


    private int mShodowColor_start = 0xdfffffff;
    private int mShodowColor_middle = 0x5fffffff;
    private int mShodowColor_end = 0x00ffffff;

    public static final int DEFAULT_TEXT_COLOR = 0xFF333333;
    public static final int DEFAULT_TEXT_SIZE = 16;

    // Text settings
    private int textColor = DEFAULT_TEXT_COLOR;
    private int textSize = DEFAULT_TEXT_SIZE;
    public int indent;
    private Context mContext;

    WheelView mWheel1;
    WheelView mWheel2;
    WheelView mWheel3;
    public WheelAdapter mAdapter1;
    public WheelAdapter mAdapter2;
    private WheelAdapter mAdapter3;
    public List<T> mDatas1;
    public List<T> mDatas2;
    public List<T> mDatas3;
    int mCurrentValue1;
    int mCurrentValue2;
    int mCurrentValue3;

    public BaseWheelController() {
        onPreExecute();
        mDatas1 = getItems1();
        mDatas2 = getItems2();
        mDatas3 = getItems3();
    }


    /**
     * 标识滚轮之间是否联动
     * 并且只支持下级滚轮关联上级滚轮
     * 例如子类为省/市/区三级滚轮时  即区随市而动 市随省而动
     */
    private boolean mWheelLinkScroll = false;


    public void bindView(LinearLayout container) {
        container.removeAllViews();
        container.setOrientation(LinearLayout.HORIZONTAL);
        mContext = container.getContext();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);

        mWheel1 = new WheelView(mContext);
        mWheel1.setLayoutParams(params);
        mWheel1.addChangingListener(this);
        mWheel1.setShadowColor(mShodowColor_start, mShodowColor_middle, mShodowColor_end);
        container.addView(mWheel1);
        mWheel1.setViewAdapter(mAdapter1 = new WheelAdapter(mDatas1));

        if (!ListUtils.isEmpty(mDatas2)) {
            mWheel2 = new WheelView(mContext);
            mWheel2.setLayoutParams(params);
            mWheel2.addChangingListener(this);
            mWheel2.setShadowColor(mShodowColor_start, mShodowColor_middle, mShodowColor_end);
            container.addView(mWheel2);

            mWheel2.setViewAdapter(mAdapter2 = new WheelAdapter(mDatas2));
        }

        if (!ListUtils.isEmpty(mDatas3)) {
            mWheel3 = new WheelView(mContext);
            mWheel3.setLayoutParams(params);
            mWheel3.addChangingListener(this);
            mWheel3.setShadowColor(mShodowColor_start, mShodowColor_middle, mShodowColor_end);
            container.addView(mWheel3);
            mWheel3.setViewAdapter(mAdapter3 = new WheelAdapter(mDatas3));
        }

    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        // TODO Auto-generated method stub
        if (wheel == mWheel1) {
            mCurrentValue1 = newValue;
            if(mWheelLinkScroll && !ListUtils.isEmpty(mDatas2)){
                resetWheel(0,newValue);
            }
        } else if (wheel == mWheel2) {
            mCurrentValue2 = newValue;
            if(mWheelLinkScroll && !ListUtils.isEmpty(mDatas3)){
                resetWheel(1,newValue);
            }
        } else if (wheel == mWheel3) {
            mCurrentValue3 = newValue;
        }
        //Log.d("zhangbo", "mCurrentValue1 == " + mCurrentValue1 + " mCurrentValue2==" + mCurrentValue2 + " mCurrentValue3==" + mCurrentValue3);
    }

    /**
     * 实现Wheel之间的联动
     * @param wheel 当前滚动的Wheel
     * @param wheelValue 当前滚动的Wheel值
     */
    private void resetWheel(int wheel,int wheelValue){
        if(wheel==0){
            linkRefresh(mDatas2,wheel,wheelValue);
            mAdapter2.notifyDataChangedEvent();
            mWheel2.setCurrentItem(0);
            mCurrentValue2 = 0;
            if(!ListUtils.isEmpty(mDatas3)){ //三级联动
                resetWheel(1,0);
            }
        }else if(wheel==1){
            linkRefresh(mDatas3,wheel,wheelValue);
            mAdapter3.notifyDataChangedEvent();
            mWheel3.setCurrentItem(0);
            mCurrentValue3 = 0;
        }
    }

    /**
     * 需要刷新滚轮数据时调用
     */
    public void refreshWheel(List<T> list,int wheelIndex){
        if(ListUtils.isEmpty(list))return;
        if(wheelIndex==0 && mDatas1!=null){
            mDatas1.clear();
            mDatas1.addAll(list);
            mAdapter1.notifyDataChangedEvent();
        }else if(wheelIndex==1 && mDatas2!=null){
            mDatas2.clear();
            mDatas2.addAll(list);
            mAdapter2.notifyDataChangedEvent();
        }else if(wheelIndex==2 && mDatas3!=null){
            mDatas3.clear();
            mDatas3.addAll(list);
            mAdapter3.notifyDataChangedEvent();
        }
    }



    /**
     * 滚轮联动开关
     */
    public void setScrollLinked(boolean linked){
        mWheelLinkScroll = linked;
    }


    /**
     * 覆写此方法 返回一级滚轮数据集合  不能为空
     */
    public abstract List<T> getItems1();

    /**
     * 覆写此方法 返回二级滚轮数据集合  不用可为空
     */
    public abstract List<T> getItems2();

    /**
     * 覆写此方法 返回三级滚轮数据集合 不用可为空
     */
    public abstract List<T> getItems3();

    /**
     * 可覆写此方法做数据初始化操作
     */
    public void onPreExecute(){
        //这里添加的这三行是用于处理确认订单页页面中的时间选择，因时间段为不固定的形式，第一次初始化的时候不能完整的现实，所以才加添的代码，对其他地方的复用不影响
        mDatas1 = getItems1();
        mDatas2 = getItems2();
        mDatas3 = getItems3();
    }

    /**
     * 覆写此方法并将mWheelLinkScroll置为true 通过对targetDatas的新增和删除操作来更新WheelView
     * @param targetDatas    需要更新的数据列表
     * @param scrollingWheel 当前滑动的滚轮
     * @param newValue       当前滑动得到滚轮值
     */
    public void linkRefresh(List<T> targetDatas,int scrollingWheel, int newValue){
        // do nothing...
    }




    public int[] getCurrentValues() {
        int[] values = {mCurrentValue1,mCurrentValue2,mCurrentValue3};
        return values;
    }

    /**
     * 自定义当前滚轮值
     * @param values
     */
    public void setCurrentValues(int ...values){
        int value1 = values[0];
        int value2 = values.length>1?values[1]:0;
        int value3 = values.length>1?values[2]:0;

        if(value1>0 && value1<mDatas1.size()){
            mWheel1.setCurrentItem(value1);
            mCurrentValue1 = value1;
        }

        if(mDatas2!=null && value2>0 && value2<mDatas2.size()){
            mWheel2.setCurrentItem(value2);
            mCurrentValue2 = value2;
        }

        if(mDatas3!=null && value3>0 && value3<mDatas3.size()){
            mWheel3.setCurrentItem(value3);
            mCurrentValue3 = value3;
        }

    }


    /**
     * 滚轮适配器
     * @param <T>
     */
    private class WheelAdapter<T> extends AbstractWheelAdapter {

        private List<T> items;


        public WheelAdapter(List<T> items) {
            this.items = items;
        }

        @Override
        public int getItemsCount() {
            return items == null ? 0 : items.size();
        }

        @Override
        public View getItem(int index, View convertView, ViewGroup parent) {
            if (index >= 0 && index < getItemsCount()) {
                if (convertView == null) {
                    convertView = new TextView(mContext);
                }
                TextView textView = (TextView) convertView;
                setTextConfigure(textView);
                String content = items.get(index).toString();
                textView.setText(content);
                indent=index;

                return convertView;

            }
            return null;
        }

        protected void setTextConfigure(TextView view) {
            view.setTextColor(textColor);
            view.setGravity(Gravity.CENTER);
            view.setTextSize(textSize);
            view.setEllipsize(TextUtils.TruncateAt.END);
            view.setLines(1);
            view.setHeight(PxUtils.dp2px(50,mContext));
        }


    }
}
